package model;

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

public abstract class Subject {
	
	
	private String id;
	private CopyOnWriteArraySet<Observer> mObservers;
	
	public Subject() {
		mObservers = new CopyOnWriteArraySet<Observer>();
	}
	
	public Subject(String newID) {
		this();
		id = newID;
	}
	
	
	public String getID() {
		return id;
	}
	
	/**
	 * Registers a new Observer that is interested in being
	 * notified when this Subject has changed in some way.
	 * 
	 * @param observer	Observer	The Observer to notify on change.
	 */
	public void register(Observer observer) {
		mObservers.add(observer);
	}
	
	/**
	 * Unregisers a previously registered Observer so that
	 * changes in this <code>Subject</code> no longer notify
	 * the <code>Observer</code>
	 * 
	 * @param observer	Observer	The Observer to remove from the list.
	 */
	public void unregister(Observer observer) {
		mObservers.remove(observer);
	}
	
	/**
	 * Called from a <code>Subject</code> to notify the interested
	 * Observers that something has changed within the object.
	 */
	public void notifyObservers() {
		for (Observer o : mObservers) {
			o.update(this);
		}
	}

}
